import lejos.nxt.ADSensorPort;
import lejos.nxt.Button;
import lejos.nxt.LightSensor;

/**
 * Class to test the robot's light sensor.
 *
 * @author Christoph Ammann
 */
public class LS {

	/* Stores the number of samples */
	private static final int COUNT = 50;
	/* Stores a reference to the LightSensor */
	private LightSensor ls_sen;

	/**
	 * Calls the LightSensor constructor.
	 *
	 * @param port the connected port, e.g. SensorPort.S1
	 * @param floodlight true enables floodlight
	 * @see LightSensor#LightSensor(ADSensorPort, boolean)
	 */
	public LS(ADSensorPort port, boolean floodlight) {
		ls_sen = new LightSensor(port, floodlight);
	}

	/**
	 * Calibrates the low and high value by placing the light sensor first next to
	 * the line and second next to the line respectively.
	 *
	 * @see LightSensor#calibrateLow()
	 * @see LightSensor#calibrateHigh()
	 */
	public void calibrate() {
		System.out.println("NEXT TO the line -> press a button");
		Button.waitForPress();
		ls_sen.calibrateLow();
		System.out.println("ABOVE the line -> press a button");
		Button.waitForPress();
		ls_sen.calibrateHigh();
		System.out.println("calibration done -> press a button");
		Button.waitForPress();
	}

	/**
	 * Displays a number of COUNT normalized samples obtained from the light sensor.
	 * Results are normalized between low and high as obtained by the calibration
	 * process.
	 *
	 * @see LightSensor#readValue()
	 * @see LS#calibrate()
	 */
	public void testLightSensor_Calibrated() {
		/* turn on floodlight */
		ls_sen.setFloodlight(true);
		/* display a number of COUNT samples in the range of low and high */
		for(int i=0; i<COUNT; i++) {
			System.out.println(new Integer(ls_sen.readValue()).toString());
			try {
				Thread.sleep(750);
			} catch (InterruptedException e) {
			}
		}
	}

	/**
	 * Displays a number of COUNT normalized samples obtained from the light sensor.
	 * Results are normalized between 0 and 1023.
	 *
	 * @see LightSensor#readNormalizedValue()
	 */
	public void testLightSensor_Normalized() {
		/* turn on floodlight */
		ls_sen.setFloodlight(true);
		/* display a number of COUNT samples in the range of 0 and 1023 */
		for(int i=0; i<COUNT; i++) {
			System.out.println(new Integer(ls_sen.readNormalizedValue()).toString());
			try {
				Thread.sleep(750);
			} catch (InterruptedException e) {
			}
		}
	}

}
